﻿using System;
using System.Collections.Generic;
using System.Text;
using LW.DB;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace iLangl.Security
{
	public class AuthenticationServiceDAL
	{
		public static string ConnectionString = string.Empty;
		public AuthenticationServiceDAL()
		{
			ConnectionString = ConfigurationManager.ConnectionStrings["iLanglConnectionString"].ConnectionString;
		}


		public String CreateSession(Guid userId)
		{
			DBProvider provider = new DBProvider(ConnectionString);
			object result = provider.ExecuteScalar("iLangl_sp_CreateSession",
				CommandType.StoredProcedure,
				new SqlParameter("@UserId", userId));

			if (result == null)
				return String.Empty;

			return ((Guid)result).ToString();
		}

		public Session ReadSession(String sessionId)
		{

			DBProvider provider = new DBProvider(ConnectionString);

			using (SqlDataReader reader = provider.ExecuteReader("iLangl_sp_ReadSession",
			CommandType.StoredProcedure,
			new SqlParameter("@SessionId", sessionId)))
			{

				if (reader.Read())
				{
					Session result = new Session();
					result.SessionId = ((Guid)reader["SessionId"]).ToString();
					result.Time = (DateTime)reader["Time"];
					result.UserId = (Guid)reader["UserId"];
					result.UserName = (String)reader["UserName"];
					return result;
				}
				else
				{
					return null;
				}
			}

		}

	}
}
